package com.hr.dao;

import com.hr.model.Position;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface PositionDAO
{

    @Results(id = "position",value = {
            @Result(id = true, property = "id", column = "p_id"),
            @Result(property = "name", column = "p_name"),
            @Result(property = "privilege", column = "p_privilege"),
            @Result(property = "start", column = "p_start"),
            @Result(property = "end", column = "p_end"),
            @Result(property = "salary", column = "p_salary"),

            @Result(property = "company", column = "c_id",one=@One(select = "com.hr.dao.CompanyDAO.getCompanyByCId")),
            @Result(property = "department", column = "dep_id",one=@One(select = "com.hr.dao.DepartmentDAO.getDepartmentByDepId"))
    })
    @Select("select * from position where p_id=#{pid}")
    Position getPositionByPId(@Param("pid")int pid);

    @ResultMap("position")
    @Select("select * from position where a_id=#{aid}")
    List<Position> getPositionsByAId(@Param("aid")int aid);



    @Insert("insert into position (a_id, dep_id, c_id, p_name, p_privilege, p_start, p_end, p_salary) " +
            "VALUES (#{aid},#{position.department.id},#{position.company.id},#{position.name},#{position.privilege},#{position.start},#{position.end},#{position.salary})")
    void insertPositon(@Param("aid")int aid,@Param("position")Position position);

    @Update("update position " +
            "set dep_id=#{position.department.id},p_name=#{position.name}, p_privilege=#{position.privilege}, p_start=#{position.start}, p_end=#{position.end}, p_salary=#{position.salary} " +
            "where p_id=#{position.id}")
    void updatePositon(@Param("position")Position position);

}
